<template>
    <h2>商品类目</h2>
    <div style="text-align: left; border-bottom: 1px solid gray; padding-bottom: 10px;">
        <el-tree :props="config" :data="categoryList" default-expand-all node-key="id" ref="treeRef"
            :highlight-current="true" :expand-on-click-node="false" @node-click="handleClickNode">
            <template #default="{ node, data }">
                <span class="custom-tree-node">
                    <span>{{ node.label }}</span>
                    <span>
                        <el-button size="small" type="primary" @click.stop="handleEdit(node, data)">
                            修改
                        </el-button>

                        <el-button size="small" type="danger" @click.stop="handleDelete(node, data)">
                            删除
                        </el-button></span>
                </span>

            </template>
        </el-tree>
    </div>
    <el-form :model="categoryForm" label-width="120px" style="margin-top: 10px;">
        <el-form-item label="分类编号">
            <el-input v-model="categoryForm.isbn" style="width: 80%" />
        </el-form-item>
        <el-form-item label="分类名称">
            <el-input v-model="categoryForm.cateName" style="width: 80%" />
        </el-form-item>
        <el-form-item label="分类描述">
            <el-input v-model="categoryForm.remark" style="width: 80%" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="saveCategoryForm">保存</el-button>
            <el-button>清空</el-button>
        </el-form-item>
    </el-form>

</template>

<script setup>
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';

var pNode = null

function handleDelete(node, data) {
    if (data.children.length != 0) {
        ElMessage.warning("该节点存在叶子节点,请选中叶子节点删除........");
        return;
    }
    var id = data.id;
    axios.get("http://localhost:8081/deleteCategory/" + id)
        .then((resp) => {
            if (response.data.code == 200) {
                //刷新商品类目树
                queryCategoryTree()
                ElMessage({ message: resp.data.msg, type: 'success' });
            } else {
                ElMessage.error(resp.data.msg)
            }

        })
        .catch((error) => {
            console.log(error);
        })
}

function handleEdit(node, data) {
    axios.get("http://localhost:8081/loadCategory/" + data.id)
        .then((response) => {
            //将响应数据赋值给表单
            categoryForm.isbn = response.data.isbn;
            categoryForm.cateName = response.data.cateName;
            categoryForm.remark = response.data.remark;
            categoryForm.id = data.id;
        })
        .catch((error) => {
            console.log(error);
        });
}

function saveCategoryForm() {
    var url = "http://localhost:8081/saveCategory"

    if (categoryForm.id == 0) {
        if (!pNode) {
            ElMessage("请选中某个父节点");
            return;
        }
        if (pNode.data.pid != 0) {
            ElMessage("请选中一级父节点");
            return;
        }
        categoryForm.pid = pNode.id; //指定当前新增节点的父节点
    } else {
        url = "http://localhost:8081/updateCategory"
    }

    //发送ajax请求保存商品类目数据
    axios.post(url, categoryForm)
        .then((resp) => {
            if (resp.data.code == 200) {
                queryCategoryTree();
                //清空表单
                categoryForm.isbn = '';
                categoryForm.cateName = '';
                categoryForm.remark = '';
                categoryForm.id = 0
                pNode = null;
                ElMessage({ message: resp.data.msg, type: 'success' });
            } else {
                ElMessage.error(resp.data.msg)
            }
        })
        .catch((error) => {
            console.log(error)
        })
}


const treeRef = ref()
const categoryList = ref([])
const config = {
    id: 'id',
    label: 'label',
    children: 'children'
}

const categoryForm = reactive({
    isbn: '',
    cateName: '',
    remark: ''
})

function queryCategoryTree() {
    axios.get("http://localhost:8081/categoryList")
        .then((response) => {
            categoryList.value = response.data;
        })
        .catch((error) => {
            console.log(error);
        })
}

onMounted(() => {
    queryCategoryTree()
})

function handleClickNode(node) {
    pNode = node
}
</script>

<style scoped>
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
}
</style>